<script setup>

import {ref, onMounted, watch, computed} from 'vue';
import axios from "axios";
import { message } from 'ant-design-vue';
import moment from "moment"
import {useRoute, useRouter} from 'vue-router'

// 查询
const router = useRouter();
const pageSize = ref(10);
const pageNo = ref(1)
const items = ref([]);
const loading = ref(false);
const totalItems = ref(0);
const pagination = computed(() => ({
    total: totalItems.value,
    current: pageNo.value,
    pageSize: pageSize.value
})) 
const queryParams = ref({
    startTime: "",
    endTime: "",
    orderNumber: ""
});
const columns = ref([
    {
        title: "购进日期",
        dataIndex: "buyDate",
        key: 'buyDate',
        resizable: true
    },
    {
        title: "供应商名称",
        dataIndex: "supplierName",
        key: 'supplierName',
        resizable: true
    },
    {
        title: "商品编码",
        dataIndex: "goodsNumber",
        key: 'goodsNumber',
        resizable: true
    },
    {
        title: "商品名称",
        dataIndex: "goodsName",
        key: 'goodsName',
        resizable: true
    },
    {
        title: "通用名称",
        dataIndex: "commonName",
        key: 'commonName',
        resizable: true
    },
    {
        title: "随货同行联号",
        dataIndex: "widthGoodsNumber",
        key: 'widthGoodsNumber',
        resizable: true
    },
    {
        title: "购进单号",
        dataIndex: "buyNumber",
        key: 'buyNumber',
        resizable: true
    },
    {
        title: "购进数量",
        dataIndex: "num",
        key: 'num',
        align: 'center'
    },
    {
        title: "批号",
        dataIndex: "batchNumber",
        key: 'batchNumber',
        align: 'center',
        resizable: true
    },
    {
        title: "有效期至",
        dataIndex: "effectiveDate",
        key: 'effectiveDate',
        resizable: true
    },
    {
        title: "结算单号",
        dataIndex: "payNumber",
        key: 'payNumber',
        resizable: true
    },
    {
        title: "发票号",
        dataIndex: "invoiceNumber",
        key: 'invoiceNumber',
        resizable: true
    },
    {
        title: "药品类型",
        dataIndex: "medicineType",
        key: 'medicineType',
        resizable: true
    },
    {
        title: "返点率(%)",
        dataIndex: "pointRate",
        key: 'pointRate',
        align: 'center'
    },
    {
        title: "税率(%)",
        dataIndex: "taxRate",
        key: 'taxRate',
        align: 'center'
    },
    {
        title: "税额(元)",
        dataIndex: "taxAmount",
        key: 'taxAmount',
        align: 'center'
    },
    {
        title: "含税均价(元)",
        dataIndex: "taxAveragePrice",
        key: 'taxAveragePrice',
        align: 'center'
    },
    {
        title: "含税总额(元)",
        dataIndex: "taxTotalAmount",
        key: 'taxTotalAmount',
        align: 'center'
    },
    {
        title: "无税均价(元)",
        dataIndex: "noTaxAveragePrice",
        key: 'noTaxAveragePrice',
        align: 'center'
    },
    {
        title: "无税总额(元)",
        dataIndex: "noTaxTotalAmount",
        key: 'noTaxTotalAmount',
        align: 'center'
    },
    {
        title: "零售价(元)",
        dataIndex: "retailPrice",
        key: 'retailPrice',
        align: 'center'
    },
    {
        title: "结算金额(元)",
        dataIndex: "payAmount",
        key: 'payAmount',
        align: 'center'
    },
    {
        title: "无税总额(元)",
        dataIndex: "noTaxTotalAmount",
        key: 'noTaxTotalAmount',
        align: 'center'
    },
    {
        title: "返点金额(元)",
        dataIndex: "pointAmount",
        key: 'pointAmount',
        align: 'center',
        fixed: "right"
    },
])
onMounted(() => {});
//计算无税总额=单价*数量
//计算含税总额=单价*数量*(1+税率)
//税额就是含税总额-无税总额
//计算返点金额=无税总额*返点率
////如果没有结算金额，则自动计算结算金额。计算结算金额=含税总额-返点金额

//无税均价就是单价
//含税均价就是含税单价
const dialogEnum = {
    update: "编辑",
    create: "新增"
}

function handleTabChange(pag){}

const selectedRowKeys = ref([])
function onSelectChange(keys ) {
    selectedRowKeys.value = keys
}

function deleteList() {
    if(!selectedRowKeys.value.length) {
        message.error("请选择数据！")
        return
    }
    let obj = {
        medicinePointIdList: Object.values(selectedRowKeys.value)
    }
    axios.post('/MedicinePoint/disableMedicinePointBatch', obj).then(res => {
        if(res.data.code == 0) {
            message.success('删除成功！')
            loadItems({page: 1, itemsPerPage: pageSize.value});
        } else {
            message.error(res.data.message)
        }
    }, err => {
        message.error(err.data.message)
    })
}

const fileList = ref([]);
function uploadFile(file) {
    var formData = new FormData();
    formData.append("file", file.file)
    loading.value = true
    axios({
        method: "post",
        url: "/MedicinePointCalc/importMedicinePointCalcList",
        data: formData,
        headers: {
            'Content-Type': "multipart/form-data"
        }
    }).then(res => {
        loading.value = false
        message.success(res.data.message)
        items.value = res.data.data
        totalItems.value = res.data.data.length
    }, err => {
        loading.value = false
        message.error(err.data.message)
    })
}

function exportTemplate() {
    let a = document.createElement("a");
    // 必须放在/static 文件目录下  使用相对路径即可
    a.href = './order.xlsx';
    a.download = '药品返点计算清单导入模板红色字段必填 .xlsx';
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    a.remove();
}

function uploadList() {
    let data = JSON.parse(JSON.stringify(items.value))
    data.forEach(record => {
        record.noTaxTotalAmount = (record.noTaxAveragePrice * record.num).toFixed(2)
        record.taxTotalAmount = (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100))).toFixed(2)
        record.taxAmount = (record.taxTotalAmount - record.noTaxTotalAmount).toFixed(2)
        record.pointAmount = (record.noTaxTotalAmount * (record.pointRate/100)).toFixed(2)
        record.payAmount = (record.taxTotalAmount - record.pointAmount).toFixed(2)
    })
    let obj = {
        medicinePointCalcList: data
    }
    loading.value = true
    axios.post("/MedicinePointCalc/addMedicinePointCalcListBatch", obj).then(res => {
        if(res.data.code == 0) {
            message.success('上传成功！')
            loading.value = false
            router.push("/medicine_order")
        } else {
            message.error(res.data.message)
            loading.value = false
        }
    }, err => {
        loading.value = false
        message.error(err.data.message)
    })
}

import { PlusOutlined, CheckOutlined, VerticalAlignBottomOutlined } from '@ant-design/icons-vue';

const totalNum = computed(() => {
    const num = items.value.reduce((sum, record) => sum + (record.noTaxAveragePrice * record.num * (record.pointRate/100)), 0)
    return num.toFixed(2);
})

function handleResizeColumn(w, col) {
  col.width = w;
}
</script>

<template>
    <a-spin size="large" :spinning="loading">
        <a-card title="药品返点计算清单">
            <template #extra>
                <a-row>
                    <a-col>
                        <a-button @click="exportTemplate">
                            <template #icon><VerticalAlignBottomOutlined /></template>
                            下载模板
                        </a-button>  
                    </a-col>
                    &nbsp;&nbsp;
                    <a-col>
                        <a-upload :file-list="fileList" :customRequest="uploadFile">
                            <a-button>
                                <template #icon><PlusOutlined /></template>
                                导入清单
                            </a-button>                
                        </a-upload>                        
                    </a-col>
                    &nbsp;&nbsp;
                    <a-col>
                        <a-button @click="uploadList">
                            <template #icon><CheckOutlined /></template>
                            上传应用
                        </a-button>                        
                    </a-col>
                </a-row>
            </template>
            <a-table
            @resizeColumn="handleResizeColumn"
            :dataSource="items"
            :columns="columns"
            :pagination="pagination"
            :scroll="{ x: 3000 }"
            @change="handleTabChange">
                <template #bodyCell="{ column, text, record }">
                    <tempalte v-if="column.dataIndex == 'pointRate'">
                        <a-input v-model:value="record.pointRate"></a-input>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'taxRate'">
                        <a-input v-model:value="record.taxRate"></a-input>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'taxAveragePrice'">
                        <a-input v-model:value="record.taxAveragePrice"></a-input>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'noTaxAveragePrice'">
                        <a-input v-model:value="record.noTaxAveragePrice"></a-input>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'retailPrice'">
                        <a-input v-model:value="record.retailPrice"></a-input>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'num'">
                        <a-input v-model:value="record.num"></a-input>
                    </tempalte>
                    <template v-if="column.dataIndex == 'noTaxTotalAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'taxTotalAmount'">
                        <!-- <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100))).toFixed(2) }}</span> -->
                        <span>{{ (record.taxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'taxAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'pointAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'payAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2) }}</span>
                    </template>
                </template>
                <template #summary>
                    <a-table-summary :fixed="'bottom'">
                        <a-table-summary-row>
                            <a-table-summary-cell :index="0">
                                <div style="font-weight: 700;">返点总额</div>
                            </a-table-summary-cell>
                            <a-table-summary-cell :index="1" :col-span=22></a-table-summary-cell>
                            <a-table-summary-cell :index="23">
                                <div style="text-align: center;font-weight: 700;">{{ totalNum }}</div>
                            </a-table-summary-cell>
                        </a-table-summary-row>
                    </a-table-summary>
                </template>
            </a-table>            
        </a-card>
    </a-spin>
</template>

<style scoped lang="less">

</style>
